/*
 * Copyright (c) 2001-2019 GuaHao.com Corporation Limited. All rights reserved.
 * This software is the confidential and proprietary information of GuaHao Company.
 * ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with GuaHao.com.
 */
package com.sright.bcard.common.service.share.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author pro
 * @version V1.0
 * @since 2019-02-21 17:34
 */
public class JsonUtil {

    /**
     * jackson json
     */
    private static final ObjectMapper jacksonMapper = new ObjectMapper();

    static {
        jacksonMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        jacksonMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        jacksonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        jacksonMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    }

    /**
     * String转换为List<String>
     * @param jsonString
     * @return
     */
    public static List<String> strJson2ListString(String jsonString){
        List<String> arrList = new ArrayList<>();
        if(StringUtils.isBlank(jsonString)){
            return arrList;
        }
        JSONArray jsonArr = JSONArray.parseArray(jsonString);
        for (int i = 0; i < jsonArr.size(); ++i) {
            Object a = jsonArr.get(i);
            arrList.add(a.toString());
        }
        return arrList;
    }

    /**
     * List<String>转换为String
     * @param list
     * @return
     */
    public static String listString2strJson(List<String> list){
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return JSONObject.toJSONString(list);
    }

    /**
     * String转换为List<Long>
     * @param jsonString
     * @return
     */
    public static List<Long> strJson2ListLong(String jsonString){
        List<Long> arrList = new ArrayList<>();
        if(StringUtils.isBlank(jsonString)){
            return arrList;
        }
        JSONArray jsonArr = JSONArray.parseArray(jsonString);
        for (int i = 0; i < jsonArr.size(); ++i) {
            Object a = jsonArr.get(i);
            arrList.add(Long.valueOf(a.toString()));
        }
        return arrList;
    }

    /**
     * List<Long>转换为String
     * @param list
     * @return
     */
    public static String listLong2strJson(List<Long> list){
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return JSONObject.toJSONString(list);
    }

    /**
     * String转换为List<Integer>
     * @param jsonString
     * @return
     */
    public static List<Integer> strJson2ListInteger(String jsonString){
        List<Integer> arrList = new ArrayList<>();
        if(StringUtils.isBlank(jsonString)){
            return arrList;
        }
        JSONArray jsonArr = JSONArray.parseArray(jsonString);
        for (int i = 0; i < jsonArr.size(); ++i) {
            Object a = jsonArr.get(i);
            arrList.add(Integer.valueOf(a.toString()));
        }
        return arrList;
    }

    /**
     * List<Integer>转换为String
     * @param list
     * @return
     */
    public static String listInteger2strJson(List<Integer> list){
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return JSONObject.toJSONString(list);
    }

    /**
     * 把JSON字符串转化为集合对象
     *
     * @param <C>
     * @param <T>
     * @param json
     * @param collectionType 集合的类型
     * @param elementClass   元素的类型
     * @return
     */
    public static <C extends Collection<T>, T> Collection<T> json2beanCollection(String json, Class<C> collectionType,
                                                                                 Class<T> elementClass) {
        try {
            if (StringUtils.isEmpty(json)) {
                return null;
            }
            json = json.trim();
            if (!json.startsWith("[")) {
                json = "[" + json;
            }
            if (!json.endsWith("]")) {
                json = json + "]";
            }
            return jacksonMapper.readValue(json,
                    TypeFactory.defaultInstance().constructCollectionType(collectionType, elementClass));
        } catch (Exception e) {
            throw new IllegalArgumentException("invalid json for " + collectionType.getName()
                    + "[" + elementClass.getName() + "]:" + json, e);
        }
    }

    /**
     * 转换对象成json字符串
     *
     * @param obj
     * @return
     */
    public static String bean2json(Object obj) {
        try {
            return jacksonMapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 转换json字符串成clazz指定类型的对象
     *
     * @param <T>
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T json2bean(String json, Class<T> clazz) {
        try {
            if (StringUtils.isEmpty(json)) {
                return null;
            }
            return jacksonMapper.readValue(json, clazz);
        } catch (Exception e) {
            throw new IllegalArgumentException("invalid json for " + clazz.getName() + ":" + json, e);
        }
    }

}
